extends CanvasLayer

var KeyEvents: Array
@export var displayDebugPannel: bool

@onready var ability_toggler: VBoxContainer = $AbilityToggler
@onready var check_box: CheckBox = $CheckBox

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	#$CheckBox.toggled = displayDebugPannel
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.

func add_line(label, varible):
	$Info/Label.text += label + " " + str(varible) + "\n"
	

func _process(delta: float) -> void:
	if displayDebugPannel:
		$Info.show()
		ability_toggler.show()
		$Info/Label.text = ""
		add_line("FPS", round(1 / delta))
		add_line("Player HP", GlobalGameData.player_HP)
		add_line("Player Pos", get_node("%Player").position)
		add_line("Player Dir", get_node("%Player").direction)
		add_line("Delay Jump Timer", get_node("%Player").delayJumpTimer)
		add_line("Jump State", get_node("%Player").currentJumpState)
		add_line("Dialog list", GlobalGameData.dialog_showing_list)
		add_line("Abilities", GlobalGameData.player_abilities)
		$Info/KeyHistory.text = "Input History" + "\n" + "\n".join(KeyEvents)
	else:
		$Info.hide()
		ability_toggler.hide()


func _input(event):
	var m := event as InputEventMouse
	if m:  # ignore mouse events
		return
	
	KeyEvents.insert(0, "> " + event.as_text())
	if len(KeyEvents) > 10:
		KeyEvents.pop_back()


func _on_check_box_toggled(toggled_on: bool) -> void:
	displayDebugPannel = toggled_on
	check_box.focus_mode =Control.FOCUS_NONE
	


func _on_dash_pressed() -> void:
	if GlobalGameData.player_abilities.has(GlobalGameData.Abilities.DASH):
		GlobalGameData.player_abilities.erase(GlobalGameData.Abilities.DASH)
	else:
		GlobalGameData.player_abilities.append(GlobalGameData.Abilities.DASH)


func _on_double_jump_pressed() -> void:
	if GlobalGameData.player_abilities.has(GlobalGameData.Abilities.DOUBLE_JUMP):
		GlobalGameData.player_abilities.erase(GlobalGameData.Abilities.DOUBLE_JUMP)
	else:
		GlobalGameData.player_abilities.append(GlobalGameData.Abilities.DOUBLE_JUMP)


func _on_wall_jump_pressed() -> void:
	if GlobalGameData.player_abilities.has(GlobalGameData.Abilities.WALL_JUMP):
		GlobalGameData.player_abilities.erase(GlobalGameData.Abilities.WALL_JUMP)
	else:
		GlobalGameData.player_abilities.append(GlobalGameData.Abilities.WALL_JUMP)


func _on_delete_save_pressed() -> void:
	pass
